package utils;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import play.Logger;

public class NovaDateUtil{
/** 
     * 从字符串类型获取日期Date 
     * @param dateStr  日期字符串，如：20080321 
     * @param format  格式化模式，如yyyymmdd 和dateStr保持一致的格式 
     * @return 返回一个Date类型日期 
     * @throws Exception 
     */  
    public static Date getDateFromString(String dateStr, String format) {  
  
        if (dateStr == null || format == null) {  
            try {  
                throw new Exception("数据类型异常" + dateStr + "|" + format);  
            } catch (Exception e) {  
                System.err.println("数据类型异常");  
            }  
        }  
  
        SimpleDateFormat df = new SimpleDateFormat(format);  
        Date date;  
        try {  
            date = df.parse(dateStr);  
            return date;  
        } catch (Exception ex) {  
            return new Date();  
        }  
    }
    
    public static java.sql.Date getSQLDateFromString(String dateStr, String format) {  
  
        if (dateStr == null || format == null) {  
            try {  
                throw new Exception("数据类型异常" + dateStr + "|" + format);  
            } catch (Exception e) {  
                System.err.println("数据类型异常");  
            }  
        }  
  
        SimpleDateFormat df = new SimpleDateFormat(format);  
        java.sql.Date date;  
        try {  
            date = new java.sql.Date(df.parse(dateStr).getTime());
            return date;  
        } catch (Exception ex) {  
            return new java.sql.Date(System.currentTimeMillis());  
        }  
    }
  
    /** 
     * Calendar类型转换成Date类型日期 
     *  
     * @param calendar 
     * @return 
     */  
    public static Date getDateFromCalendar(Calendar calendar) {  
        return calendar.getTime();  
    }  
  
    /** 
     * Timestamp类型转换成Date类型日期 
     *  
     * @param timestamp 
     * @param format 
     * @return 
     */  
    public static Date getDateFromTimestamp(Timestamp timestamp, String format) {  
        return getDateFromString(timestamp.toString(), format);  
    }
    
    public static Timestamp getTimestampFromString(String dateStr, String formatStr){
    	SimpleDateFormat sdf=new SimpleDateFormat(formatStr);
		Timestamp ts=null;
		try {
			ts=new Timestamp(sdf.parse(dateStr).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return ts;
    }
  
    /** 
     * String类型转换成Timestamp类型日期 
     *  
     * @param dateStr 
     *            日期字符串 
     * @return 
     * @throws Exception 
     */  
    public static Timestamp getTimestampFromString(String dateStr) {  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00.000"); 
    	//出票格式 不优化 从新设置
//    	  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");  
//      2011.12.9
        return Timestamp.valueOf(sdf.format(getDateFromString(dateStr,  
                "yyyyMMddHHmmss")));  
//        return Timestamp.valueOf(sdf.format(getDateFromString(dateStr,  
//        "yyyyMMdd")));  
    }  
  
    /** 
     * Calendar类型转换Timestamp类型的日期 
     *  
     * @param calendar 
     *            参数是Calendar类型 
     * @return 
     */  
    public static Timestamp getTimestampFromCalendar(Calendar calendar) {  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00.000");  
        return Timestamp.valueOf(sdf.format(calendar.getTime()));  
    }  
  
    /** 
     * Date类型转换Timestamp类型的日期 
     *  
     * @param date 
     * @return 
     */  
    public static Timestamp getTimestampFromDate(Date date) {  
        try {  
            return getTimestampFromString(getStringFromDate(date, "yyyy-MM-dd"));  
        } catch (Exception e) {  
            // System.out.println("获取日期出错");  
            return null;  
        }  
    }  
    
    /** 
     * Date类型转换Timestamp类型的日期 
     *  
     * @param date 
     * @return 
     */  
    public static Timestamp getTimestampFromDate(Date date, String format) {  
    	try {  
    		return getTimestampFromString(getStringFromDate(date, format));  
    	} catch (Exception e) {  
    		// System.out.println("获取日期出错");  
    		return null;  
    	}  
    }  
  
    /** 
     * String(字符串类型)转换成Calendar类型的日期 
     *  
     * @param dateStr 
     * @return 
     */  
    public static Calendar getCalendarFromString(String dateStr) {  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(getDateFromString(dateStr, "yyyymmdd"));  
        return calendar;  
    }  
  
    /** 
     * Date类型转换成Calendar类型日期 
     *  
     * @param date 
     * @return 
     */  
    public static Calendar getCalendarFromDate(Date date) {  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        return calendar;  
    }  
  
    /** 
     * Timestamp类型转换成Calendar类型日期 
     *  
     * @param timestamp 
     * @return 
     */  
    public static Calendar getCalendarFromTimestamp(Timestamp timestamp) {  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(getDateFromTimestamp(timestamp, "yyyymmdd"));  
        return calendar;  
    }  
  
    /** 
     * Date类型转换成String(字符串)类型的日期 
     *  
     * @param date 
     * @return 
     */  
    public static String getStringFromDate(Date date, String format) {  
        SimpleDateFormat sdf = new SimpleDateFormat(format);  
        return sdf.format(date);  
    }  
    public static String getStringFromDate(java.sql.Date date, String format){
    	SimpleDateFormat sdf = new SimpleDateFormat(format);  
        return sdf.format(date);
    }
    public static String getStringFromTime(Time time, String format){
    	SimpleDateFormat sdf = new SimpleDateFormat(format);  
        return sdf.format(time);
    }
    public static String formatTimestamp(Timestamp date, String format){
    	 SimpleDateFormat sdf = new SimpleDateFormat(format);  
         return sdf.format(date);  
    }
    /** 
     * Calendar类型转换成String类型的日期 
     *  
     * @param celendar 
     * @return 
     */  
    public static String getStringFromCalendar(Calendar celendar, String format) {  
        SimpleDateFormat sdf = new SimpleDateFormat(format);  
        return sdf.format(celendar.getTime());  
    }  
  
    /** 
     * Timestamp类型转换成String类型的日期 
     *  
     * @param timestamp 
     * @return 
     */  
    public static String getStringFromTimestamp(Timestamp timestamp) {  
        return timestamp.toString();  
    }  
  
    /** 
     * 格式化format 
     *  
     * @param dateStr 
     * @param format 
     * @return 
     */  
    public static String getStringFormat(String dateStr, String format) {  
  
        SimpleDateFormat sdf = new SimpleDateFormat(format);  
        String result = "";  
  
        try {  
            result = sdf.format(dateStr);  
        } catch (Exception e) {  
        }  
  
        return result;  
  
    }  
  
    /** 
     * 是否为某个月的最后一天 
     *  
     * @param days 
     * @param month 
     * @param year 
     * @return 
     */  
    public static boolean isLastDayOfMonth(int days, int month, int year) {  
        month = month - 1;  
        int monthCompare = month;  
        Calendar calendar = Calendar.getInstance();  
        // calendar.set(year, month, days);  
        calendar.set(Calendar.DAY_OF_MONTH, days);  
        calendar.set(Calendar.MONTH, month);  
        calendar.set(Calendar.YEAR, year);  
        // System.out.println("月份："+(calendar.get(Calendar.MONTH)+1));  
        // System.out.println("几号："+calendar.get(Calendar.DAY_OF_MONTH));  
        calendar.add(Calendar.DAY_OF_MONTH, 1);  
        // System.out.println("月份："+(calendar.get(Calendar.MONTH)+1));  
        // System.out.println("几号："+calendar.get(Calendar.DAY_OF_MONTH));  
        if (monthCompare != calendar.getInstance().MONTH) {  
            return true;  
        }  
        return false;  
    }  
  
    //    
    // /**  
    // * @param args  
    // * @throws ParseException  
    // */  
    // public static void main(String[] args) throws ParseException {  
    // DateTool dateTool=new DateTool();  
    // //测试是否为最后一天。。。  
    // boolean b=dateTool.isLastDayOfMonth(29, 02, 2007);  
    // if(b){s  
    // System.out.println("最后一天");  
    // }else{  
    // System.out.println("不是最后一天");  
    // }  
    // Date date=new Date();  
    // SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");  
    // sdf.format(date);  
    //        
    // Date date1=new Date();  
    //        
    // Date date2=new Date();  
    //        
    // System.out.println(getTimestampFromDate(date1).toString());  
    // }  
  
    /** 
     * 获取当前的月份的几号 
     *  
     * @return 
     */  
    public static int getCurrentDayOfMonth() {  
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);  
    }  
  
    /** 
     * 获取当前的月份 
     *  
     * @return 
     */  
    public static int getCurrentMonth() {  
        return Calendar.getInstance().get(Calendar.MONTH);  
    }  
  
    /** 
     * 获取当前的年 
     *  
     * @return 
     */  
    public static int getCurrentYear() {  
        return Calendar.getInstance().get(Calendar.YEAR);  
    }  
  
    /** 
     * 获取当前的日期是一年中的第几天 
     *  
     * @return 
     */  
    public static int getCurrentDayOfYear() {  
        return Calendar.getInstance().get(Calendar.DAY_OF_YEAR);  
    }  
  
    /** 
     * 获取当前的日期在当前月中是第几周 
     *  
     * @return 
     */  
    public static int getCurrentWeekOfMonth() {  
        return Calendar.getInstance().get(Calendar.WEEK_OF_MONTH);  
    }  
  
    /** 
     * 获取当前的日期在当前年里的第几周 
     *  
     * @return 
     */  
    public static int getCurrentWeekOfYear() {  
        return Calendar.getInstance().get(Calendar.WEEK_OF_YEAR);  
    }  
  
    /** 
     * 获取指定calendar的月中的几号 
     *  
     * @param calendar 
     * @return 返回月份值，是一个int型数据 
     */  
    public static int getDayOfMonth(Calendar calendar) {  
        return calendar.get(Calendar.DAY_OF_MONTH);  
    }  
  
    /** 
     * 获取指定calendar的月份 
     *  
     * @param calendar 
     * @return 返回月份值，是一个int型数据 
     */  
    public static int getMonth(Calendar calendar) {  
        return calendar.get(Calendar.MONTH) + 1;  
    }  
  
    /** 
     * 获取指定calendar的年 
     *  
     * @param calendar 
     * @return 返回年值，是一个int型数据 
     */  
    public static int getYear(Calendar calendar) {  
        return calendar.get(Calendar.YEAR);  
    }  
  
    /** 
     * 获取指定calendar中年份中的第几天 
     *  
     * @param calendar 
     * @return 
     */  
    public static int getDayOfYear(Calendar calendar) {  
        return calendar.get(Calendar.DAY_OF_YEAR);  
    }  
  
    /** 
     * 获取指定calendar的月中的第几周 
     *  
     * @param calendar 
     * @return 
     */  
    public static int getWeekOfMonth(Calendar calendar) {  
        return calendar.get(Calendar.WEEK_OF_MONTH);  
    }  
  
    /** 
     * 获取指定calendar的年中的第几周 
     *  
     * @param calendar 
     * @return 
     */  
    public static int getWeekOfYear(Calendar calendar) {  
        return calendar.get(Calendar.WEEK_OF_YEAR);  
    }  
  
    /** 
     * 得到两个日期之间相差多少天  
     * @param beginDate  开始日期 
     * @param endDate    结束日期 
     * @return 
     */  
    public static int dateDiff(String beginDate,String endDate){  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        Date date = null;  
        try {  
            date = sdf.parse(endDate);  
        } catch (ParseException e) {  
            date = new Date();  
            e.printStackTrace();  
        }  
          
          
        long end    = date.getTime();  
        try {  
            date = sdf.parse(beginDate);  
        } catch (ParseException e) {  
            date = new Date();  
            e.printStackTrace();  
        }  
        long  begin     = date.getTime();  
          
        long day = (end-begin)/(1000*3600*24);      //除1000是把毫秒变成秒  
          
        return Integer.parseInt(Long.toString(day));          
    }  
  
    /** 
     * 日期推迟 
     *  
     * @param date 
     *            需要推迟的日期 
     * @param dayNumber 
     *            需要推迟的天数 
     * @return 返回新的日期 
     */  
    public static Date dateAdd(Date date, int dayNumber) {  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.add(Calendar.DAY_OF_MONTH, dayNumber);  
        return calendar.getTime();  
    }  
/** 
     * 得到某种类型的中间的所有日期.格式为"yyyy-MM-dd"    
     * @param beginDate 
     * @param endDate 
     * @return 
     */  
    public static String[] getDaysByRang(String beginDate,String endDate){  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
          
        //得到两个日期间相差多少天  
        int num = dateDiff(beginDate,endDate);  
        if(num<0){  
            //颠倒一下日期  
            String tmp = beginDate;  
            beginDate = endDate;  
            endDate = tmp;  
            num = 0-num;  
        }  
          
        String[] result = {} ;  
        Calendar cal = Calendar.getInstance() ;  
        try {  
            cal.setTime(sdf.parse(beginDate));  
        } catch (ParseException e) {  
            e.printStackTrace();  
        }  
          
        num = num + 1;  //把开始和结束日期都包含进去  
          
        result = new String[num];  
        for(int i=0;i<num;i++){  
            if(i>0)  cal.add(Calendar.DAY_OF_YEAR, 1);  
            result[i] = sdf.format(new Date(cal.getTimeInMillis()));  
        }  
          
        return result;  
    }  
      
    /** 
     * 得到最近num天的全部日期 
     * 说明:   
     *  1.日期是从昨天开始算的. 
     *  2.如果num=2 , 今天的日期是2008-03-14 ,则返回的结果为 2008-03-12、2008-03-13 
     * @param num 
     * @return 
     */  
    public static String[] getDaysByNum(int num){  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        String[] result = {} ;  
        Calendar cal = Calendar.getInstance() ;  
          
        //最近一周  
        result = new String[num];  
        for(int i=num;i>0;i--){  
            cal.add(Calendar.DAY_OF_YEAR, -1);  
            result[i-1] = sdf.format(new Date(cal.getTimeInMillis()));  
        }  
          
        return result;  
    }  
      
      
      
    /** 
     * 得到最近num天的全部日期 
     * 说明:   
     *  1.日期是从昨天开始算的. 
     *  2.如果num=2 , 今天的日期是2008-03-14 ,则返回的结果为 2008-03-12、2008-03-13 
     * @param num 
     * @return 
     */  
    public static String[] getDaysByNum(int num,String date){  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        String[] result = {} ;  
        Calendar cal = Calendar.getInstance() ;  
        cal.setTime(getDateFromString(date, "yyyy-MM-dd"));  
          
        //最近一周  
        result = new String[num];  
        for(int i=num;i>0;i--){  
            cal.add(Calendar.DAY_OF_YEAR, -1);  
            result[i-1] = sdf.format(new Date(cal.getTimeInMillis()));  
        }  
          
        return result;  
    }  
      
      
    /** 
     * 得到最近num天的全部日期 
     * 说明:   
     *  1.日期是从今天开始算的. 
     *  2.如果num=2 , 今天的日期是2008-03-14 ,则返回的结果为 2008-03-13、2008-03-14 
     * @param num 
     * @return 
     */  
    public static String[] getDaysByBefore(int num,String date){  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        String[] result = {} ;  
        Calendar cal = Calendar.getInstance() ;  
        cal.setTime(getDateFromString(date, "yyyy-MM-dd"));  
        cal.add(Calendar.DAY_OF_YEAR, 1);  
        //最近num的日期，包括date日期  
        result = new String[num];  
        for(int i=num;i>0;i--){    
            cal.add(Calendar.DAY_OF_YEAR, -1);  
            result[i-1] = sdf.format(new Date(cal.getTimeInMillis()));  
              
        }  
          
        return result;  
    }  
    /** 
     * 得到最近num天的全部日期 
     * 说明:   
     *  1.日期是从今天开始算的. 
     *  2.如果num=2 , 今天的日期是2012-05-24 ,则返回的结果为2012-05-25,2012-05-26 
     * @param num 
     * @return 
     */ 
    public static String[] getDaysByback(int num,String date){  
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
    	String[] result = {} ;  
    	Calendar cal = Calendar.getInstance() ;  
    	cal.setTime(getDateFromString(date, "yyyy-MM-dd")); 
    	//最近num的日期，包括date日期  
    	result = new String[num];  
    	for(int i=0;i<num;i++){    
    		result[i] = sdf.format(new Date(cal.getTimeInMillis()));  
    		cal.add(Calendar.DAY_OF_YEAR, +1);  
    		
    	}  
    	
    	return result;  
    }  
    /**
     *根据当前日期获得星期几
     * @param dt
     * @return
     */
    public static String getWeekOfDate(String dt) {
    	SimpleDateFormat   df   =   new   SimpleDateFormat( "yyyy-MM-dd");
    	Date dd = new Date();
		try {
			dd = df.parse(dt);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		Calendar cal = Calendar.getInstance();
		cal.setTime(dd);
		
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		
		return weekDays[w];
	}
    public static int getBetweenDayNumber(String dateA, String dateB) {
    	long dayNumber = 0;
    	//1小时=60分钟=3600秒=3600000
    	long mins = 60L * 1000L;
    	//long day= 24L * 60L * 60L * 1000L;计算天数之差
    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	try {
    	   java.util.Date d1 = df.parse(dateA);
    	   java.util.Date d2 = df.parse(dateB);
    	   dayNumber = (d2.getTime() - d1.getTime()) / mins;
    	} catch (Exception e) {
    	   e.printStackTrace();
    	}
    	return (int) dayNumber;
    }
    public static int getBetweenDayNumber(Date dateA, Date dateB) {
    	long dayNumber = 0;
    	//1小时=60分钟=3600秒=3600000
    	long mins = 60L * 1000L;
    	//long day= 24L * 60L * 60L * 1000L;计算天数之差
    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	try {
    		dayNumber = (dateB.getTime() - dateA.getTime()) / mins;
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	return (int) dayNumber;
    }
  /**
   * 两个时间相差多少分钟
   * @author 刘德龙
   * @param args
   * @throws ParseException
   */
 public static Long TimeDiff(String pBeginTime, String pEndTime){
	 try {
		  DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		  Date bedate = format.parse(pBeginTime);
		  Date endate = format.parse(pEndTime);
		  Calendar becal=Calendar.getInstance();
		  Calendar encal=Calendar.getInstance();
		  becal.setTime(bedate);
		  encal.setTime(endate);
		  long diffMins = (encal.getTimeInMillis()-becal.getTimeInMillis()) / (1000 * 60);  
		  return diffMins;
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
	
 }
 
 /**
	 * 计算两个日期的时间间隔
	 *
	 * @param sDate开始时间
	 *
	 * @param eDate结束时间
	 *
	 * @param type间隔类型("Y/y"--年  "M/m"--月  "D/d"--日)
	 *
	 * @return interval时间间隔
	 * */
	public static int getInterval(Date sDate, Date eDate, String type){
		//时间间隔，初始为0
		int interval = 0;

		/*比较两个日期的大小，如果开始日期更大，则交换两个日期*/
		//标志两个日期是否交换过
		boolean reversed = false;
		if(compareDate(sDate, eDate) > 0)
		{
			Date dTest = sDate;
			sDate = eDate;
			eDate = dTest;
			//修改交换标志
			reversed = true;
		}

		/*将两个日期赋给日历实例，并获取年、月、日相关字段值*/
		Calendar sCalendar = Calendar.getInstance();
		sCalendar.setTime(sDate);
		int sYears = sCalendar.get(Calendar.YEAR);
		int sMonths = sCalendar.get(Calendar.MONTH);
		int sDays = sCalendar.get(Calendar.DAY_OF_YEAR);

		Calendar eCalendar = Calendar.getInstance();
		eCalendar.setTime(eDate);
		int eYears = eCalendar.get(Calendar.YEAR);
		int eMonths = eCalendar.get(Calendar.MONTH);
		int eDays = eCalendar.get(Calendar.DAY_OF_YEAR);

		if(cTrim(type).equals("Y") || cTrim(type).equals("y")){ //年
			interval = eYears - sYears;
			if(eMonths < sMonths)
			{
				--interval;
			}
		}else if(cTrim(type).equals("M") || cTrim(type).equals("m")){ // 月
			interval = 12 * (eYears - sYears);
			interval += (eMonths - sMonths);
		}else if(cTrim(type).equals("D") || cTrim(type).equals("d")){ // 日
			interval = 365 * (eYears - sYears);
			interval += (eDays - sDays);
			//除去闰年天数
			while(sYears < eYears){
				if(isLeapYear(sYears)){
					--interval;
				}
				++sYears;
			}
		}
		//如果开始日期更大，则返回负值
		if(reversed){
			interval = -interval;
		}
		return interval;
	}
	
	/**
	  * 判定某个年份是否是闰年
	  *
	  * @param year待判定的年份
	  *
	  * @return 判定结果
	  * */
	 private static boolean isLeapYear(int year){
		 return (year%400 == 0 || (year%4 == 0 && year%100 != 0));
	 }

	/**
	* 字符串去除两头空格，如果为空，则返回""，如果不空，则返回该字符串去掉前后空格
	* @param tStr输入字符串
	* @return 如果为空，则返回""，如果不空，则返回该字符串去掉前后空格
	*/
	public static String cTrim(String tStr){
		String ttStr = "";
		if (tStr == null){
			
		}else{
			ttStr = tStr.trim();
		}
		return ttStr;
	}

	
	/**
	 * 比较两个Date类型的日期大小
	 * @param sDate开始时间
	 * @param eDate结束时间
	 * @return result返回结果(0--相同  1--前者大  2--后者大)
	 * */
	private static int compareDate(Date sDate, Date eDate){
		int result = 0;
		//将开始时间赋给日历实例
		Calendar sC = Calendar.getInstance();
		sC.setTime(sDate);
		//将结束时间赋给日历实例
		Calendar eC = Calendar.getInstance();
		eC.setTime(eDate);
		//比较
		result = sC.compareTo(eC);
		//返回结果
		return result;
	}
	
  public static void main(String[] args) throws ParseException {
	  java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm");
	  Date datetime=format.parse("2010-12-03"+" "+"09:50");
		Calendar departcal=Calendar.getInstance();
		departcal.setTime(datetime);
		
	  Calendar nowcal = Calendar.getInstance();
		nowcal.add(Calendar.DATE,new Integer("30"));//.
	}
  public static String getSysTime(){
	 return getStringFromDate( new Date(System.currentTimeMillis()),"yyyy-MM-dd");
  }
  
  public static String getCurrentTime(String format){
	  return getStringFromDate( new Date(System.currentTimeMillis()), format);
  }
  
  public static Timestamp getCurrentTimestamp(){
	  return new Timestamp(Calendar.getInstance().getTimeInMillis());
  }
  
  /**
   * @author 刘德龙
   * 获取当前时间 精确到分钟
   * @return
   */
  public static String getCurrentTime(){
		 return getStringFromDate( new Date(System.currentTimeMillis()),"yyyy-MM-dd HH:mm");
  }
  public static java.sql.Date getCurrentDate(){
	  return new java.sql.Date(Calendar.getInstance().getTimeInMillis());
  }
  public static String getCurrentStringFromDate(){
	  return new SimpleDateFormat("yyyy-MM-dd").format(getCurrentDate());
  }
  public static java.sql.Time getCurrentSQLTime(){
	  return new Time(System.currentTimeMillis());
  }
  
  /**
   * 获取当前过mine分钟后的时间 
   * @param mine
   * @return like "2013-01-14 16:32:23"
   */
  public static String getNextTime(int mine){
	  SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
      Calendar calendar = Calendar.getInstance(); 
      calendar.add(Calendar.MINUTE, mine);    //得到30分钟后 
      return sf.format(calendar.getTime());
  }
  
  /**
   * 获取当前日期所在星期的星期一
   * @author ldl
   * @param date
   * @return
   */
  public static String getMonday(Date date) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return dateFormat.format(calendar.getTime());
	} 
  public static String getCTSToFormatDate(String date2){
	try {
		java.util.Date date;
		DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
		date = df.parse(date2);
		df = new SimpleDateFormat("yyyy-MM-dd");
	    String str = df.format(date);
	    return str;
	} catch (ParseException e) {
		e.printStackTrace();
	}
	return null;
  }
  /**
	  * 处理webservice返回的不同格式的日期时间
	  * @param orgdatetime
	  * @return
	  */
	 public static Timestamp convertWebserviceDateTime(String orgdatetime){
		 if(orgdatetime!=null){
			 if(orgdatetime.contains("-")){
				 return NovaDateUtil.getTimestampFromString(orgdatetime, 
						 "yyyy-MM-dd HH:mm:ss");
			 }else{
				 return NovaDateUtil.getTimestampFromString(orgdatetime, 
						 "yyyyMMddHHmmss");
			 }
		 }
		 return null;
	 }
  
}